package com.boot.controller;

import com.boot.entity.Menu;
import com.boot.entity.User;
import com.boot.service.MenuService;
import com.boot.service.UserService;
import com.boot.vo.AuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//@RestController 直接响应文本:一般是json格式文本
@Controller //转发到页面
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;

    @GetMapping("/list")
    public String list(Model model,
                        @RequestParam(name = "current",defaultValue = "1")Integer current,
                        @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                        String userName){

        Map<String,Object> data = userService.page(current,pageSize,userName);

        model.addAttribute("data",data);
        model.addAttribute("userName",userName);

        model.addAttribute("current",current);
        return "user/list";
    }

    @GetMapping("/del")
    public String del(Long id){
        userService.del(id);
        return "redirect:/user/list";//跳转到首页 再去自动分页
    }

    @GetMapping("/add")
    public String add(Model model,Long id){
        if(id != null){
            User user = userService.getById(id);

            model.addAttribute("user",user);
        }

        return "user/add";
    }

    @PostMapping("/doAdd")
    public String doAdd(User user){

        userService.saveOrUpdate(user);

        return "redirect:/user/list";
    }

    /**
     * http:
     *  请求头:http://abc:8080?id=100&userName=张三&password=123123 传输是从请求头传输
     *  请求体:从请求体传输 我们是看不到的:请求体中的数据我们是可以任意类型的 二进制的IO流或者文本
     *          但是我们一般做项目解析数据通常使用json格式的字符串 SpringBoot【SpringMVC】封装只解析json格式的
     *          {id:100,userName:'张三',password:'123123'}
     *          [100,200,300]
     *      1、新增、修改、删除等从请求体传输
     *      2、上传文件
     *
     *      @RequestBody:从请求体中获取数据
     *
     * @param ids
     * @return
     */
    //@RequestBody:从请求体中接收数据
    @PostMapping("/delAll")
    @ResponseBody //响应文本
    public String delAll(@RequestBody List<Long> ids){

        userService.delByIds(ids);

        return "success";
    }

    @GetMapping("/auth")
    public String auth(Model model,Long id){

        //1、需要被授权的用户查询到
        User user = userService.getById(id);
        model.addAttribute("user",user);

        //2、所有的菜单查询到
        List<Menu> menus = menuService.list();

        model.addAttribute("menus",menus);

        //3、已经选中的菜单找到
        List<Menu> selectedMenus = userService.getMenusByUserId(id);

        model.addAttribute("selectedMenus",selectedMenus);

        return "user/auth";
    }

    @PostMapping("/doAuth")
    @ResponseBody
    public String doAuth(@RequestBody AuthVo authVo){
        userService.auth(authVo);

        return "success";
    }

}
